import { fillingLists } from '@/gql/filling';
import { toPublish } from '@mqtt';
import { defineStore } from 'pinia';
import { toGetCoordinates } from './coordinates';

export const useMachineStore = defineStore('machine', {
  state: () => ({
    list: [],
  }),
  getters: {
    fixedList(state) {
      return state.list.filter(({ type }) => type === 'fixed');
    },
  },
  actions: {
    toGetList() {
      toGetCoordinates(this.getList);
    },
    getList({ longitude, latitude }) {
      const payload = {
        query: fillingLists,
        variables: {
          longitude,
          latitude,
          type: '',
        },
      };
      toPublish('ql/driver/getFillingList', payload, (obj: any) => {
        const { fill_list } = obj.data;
        this.list = fill_list === null ? [] : fill_list;
      });
    },
  },
});
